if (!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.permissions.Detail")) {


    /**
     *
     */
    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.userMgt.permissions.Detail",
            jsx3.gui.Block,
            null,
            function (permissionDetail) {

				/**
				 * instance initializer
				 */
                permissionDetail.prototype.initialize = function() {
                    // set the window instance id
                    if (jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.window.Window")) {
                        this.window = this.getAncestorOfType(tibco.admin.enterpriseAssets.userMgt.window.Window)
                        if (this.window) {
                            this.setWindowId(this.window.getId());
                        }
                    }
					
                    this.name = "";
                    this.type = "UiResourceType";
                    this.id = "";
					this.nameIdArr = new Array;
					
					this.loadPermissionButtons();

                    this.editBtn = this.getDescendantOfName("btnEdit_24_L");
                    this.saveBtn = this.getDescendantOfName("btnSave_24_L");
                    this.cancelBtn = this.getDescendantOfName("btnCancel_24_L");
					this.removeBtn = this.getDescendantOfName("btnRemove_24_L");

                    var me = this;
                    this.editBtn.showEdit = function() {
                        me.showEdit();
                    }
                    this.cancelBtn.cancelEdit = function() {
                        me.cancelEdit();
                    }
                    this.saveBtn.doSave = function() {
                        me.doSave();
                    }
					this.removeBtn.doRemove = function(){
						me.removePermission();
					}

                    this.mtxView = this.getDescendantOfName("mtxView_24_L");
                    this.mtxEdit = this.getDescendantOfName("mtxEdit_24_L");
                    this.mtxOwner = this.getDescendantOfName("mtxOwner_24_L");

					

                }

                permissionDetail.prototype.subscribeAll = function() {
					var me = this;
					setTimeout(function(){
						tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_SELECTED, me, me.setDetails)
						},0);
                }

                permissionDetail.prototype.unsubscribeAll = function() {
                    tibco.admin.PubSub.unsubscribe(tibco.admin.enterpriseAssets.userMgt.EventID.MASTER_ROW_SELECTED, this);
                }

                permissionDetail.prototype.setWindowId = function(strId) {
                    this.windowId = strId
                }

                /**
                 *@param strName {String} ?the unique name of the window to create
                 **/
                permissionDetail.prototype.getWindowId = function() {
                    return this.windowId
                }

                /**
                 *	 The call back method while listening the "master.selectUserList" event
                 */
                permissionDetail.prototype.setDetails = function(objEvent) {
                    try {
                        //Set details only if the message is for you
                        if (objEvent.windowId == this.getWindowId()) {
                            this.name = objEvent.display_name;
                            this.id = objEvent.itemId;
							this.nameIdArr = objEvent.nameIdArr;
							this.showHidePermissionTip(objEvent.nameIdArr.length,objEvent.hasOwner);							
                        }
                    } catch(ex) {
                        tibco.admin.Main.logException(ex, "tibco.admin.enterpriseAssets.userMgt.permissions.Detail#setDetails");
                    }
                }

				permissionDetail.prototype.showHidePermissionTip = function(recCount,ownerFlag){
					if(ownerFlag == true){
						this.getDescendantOfName("lytDetail_24_L").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
						this.getDescendantOfName("blkTip_24_L").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
						if(recCount>1){
							this.mtxView.clearXmlData();
							this.mtxEdit.clearXmlData();
							this.mtxOwner.clearXmlData();
							this.mtxView.repaint();
							this.mtxEdit.repaint();
							this.mtxOwner.repaint();
							this.removeBtn.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);

							this.editBtn.setDynamicProperty("jsxtext",null);
							this.editBtn.setDynamicProperty("jsxtext","@addin@btnAddPermissions");
							this.editBtn.repaint();
						}
						else{
							
							this.removeBtn.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
							this.editBtn.setDynamicProperty("jsxtext",null);
							this.editBtn.setDynamicProperty("jsxtext","@addin@Btn Edit");
							this.editBtn.repaint();
							this.callgetPermissions();
						}
					}
					else{
						this.getDescendantOfName("lytDetail_24_L").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
						this.getDescendantOfName("blkTip_24_L").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);					
					}
				}

                permissionDetail.prototype.isLoaded = function(msg) {
                    /*
                    jsx3.log("Loaded resource :" + msg.resourcePath)
                    jsx3.log("namespace :" + msg.namespace)
                    jsx3.log("headers :" + msg.headers)
                    jsx3.log("exception :" + msg.exception)
                    jsx3.log("status :" + msg.status)
                    jsx3.log("message :" + msg.message)
                    */
                }


                permissionDetail.prototype.showEdit = function() {
                    var loadTo = this.window;
                    var body = tibco.admin.Main.getServer().getBodyBlock();
                    var parent = this.window.getParent();
                    while(parent) {
                        if(parent == body) {
                            loadTo = body;
                            break;
                        }
                        parent = parent.getParent();
                    }
                    var popupWin = tibco.admin.Main.loadUIResource(loadTo,"tibco.admin","tibco/admin/enterpriseAssets/userMgt/window/permissions/ui/addUsersNGroups.xml",true);
					popupWin.setWindow(this.window);
					if(this.nameIdArr.length==1)
                      popupWin.setPermission(this.type,this.name,this.id);
					else
						if(this.nameIdArr.length>1)
							popupWin.setBulkPermission(this.type,this.nameIdArr);
			    }

                permissionDetail.prototype.cancelEdit = function() {
                    var parent = this.editBtn.getAncestorOfName("lytDetail_24_L");
                    // show edit button
                    this.editBtn.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    // hide save and cancel buttons
                    parent.getDescendantOfName("btnSave_24_L").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    parent.getDescendantOfName("btnCancel_24_L").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    // hide delete columns

                    this.mtxView.getFirstChild().setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.mtxEdit.getFirstChild().setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                    this.mtxOwner.getFirstChild().setDisplay(jsx3.gui.Block.DISPLAYNONE, true);


                }

                permissionDetail.prototype.doSave = function() {
                }

                //===================== get all permissions =======================
                //call this method to begin the service call (eg.service.callgetPermissions();)
                permissionDetail.prototype.callgetPermissions = function() {
                  var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/permissions/rules/getPermissions.xml", "getPermissions", "Get Permissions");

                  //subscribe and call
                  objService.subscribe(jsx3.net.Service.ON_SUCCESS, this,this.ongetPermissionsSuccess);
                  objService.subscribe(jsx3.net.Service.ON_ERROR, this,this.ongetPermissionsError);
                  objService.subscribe(jsx3.net.Service.ON_INVALID, this,this.ongetPermissionsInvalid);
                  objService.name = this.name;
                  objService.type = this.type;
                  objService.id = this.id;
                  //alert(tibco.admin.util.ServiceHelper.getUserMgtEndpointURL());
                  objService.setEndpointURL(tibco.admin.util.ServiceHelper.getPermissionsEndpointURL());
                  objService.doCall();
                };

                permissionDetail.prototype.ongetPermissionsSuccess = function(objEvent) {
                  var sourceXML = objEvent.target.getInboundDocument();
                  this.mtxView.setSourceXML(sourceXML);
                  this.mtxEdit.setSourceXML(sourceXML);
                  this.mtxOwner.setSourceXML(sourceXML);
                  this.mtxView.repaint();
                  this.mtxEdit.repaint();
                  this.mtxOwner.repaint();
                };

                permissionDetail.prototype.ongetPermissionsError = function(objEvent) {
                  var myStatus = objEvent.target.getRequest().getStatus();
                  tibco.admin.util.Log.USERMGT.error("The service call failed. The HTTP Status code is: " + myStatus);
                };

                permissionDetail.prototype.ongetPermissionsInvalid = function(objEvent) {
                  tibco.admin.util.Log.USERMGT.error("The following message node just failed validation:\n\n" + objEvent.message);
                };



				permissionDetail.prototype.loadPermissionButtons = function() {
					this.permissions = new tibco.admin.enterpriseAssets.userMgt.permissions.SetPermissions();
					this.masterMtx = this.window.getDescendantOfName("mtxMaster_24_k");
					this.permissions.initialize("UiResourceType", "itemid", "display_name", this.masterMtx);
				}

				permissionDetail.prototype.removePermission = function() {
					var msg = new Object();
					msg.entityType = this.type;
					msg.entities = [];
					// create selected list
					var selectedNodes = this.masterMtx.getSelectedNodes();
					var selectedNumber = selectedNodes.size();
					for (var i = 0; i < selectedNumber; i++)
					{
						msg.entities.push({id: selectedNodes.get(i).getAttribute("itemid"),
								name: selectedNodes.get(i).getAttribute("display_name")});
					}

					var loadArea = this.window;
					if(this.window.getAncestorOfName("tabpaneEnterpriseAssets_20")){
						loadArea = tibco.admin.Main.getServer().getBodyBlock();
					}
					var removePermissions = tibco.admin.Main.loadUIResource(loadArea, "tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/permissions/ui/removePermissionsM.xml");
					removePermissions.initialize(msg);
				}
				



            });
}